/**
 * 
 */
package core;

import java.sql.SQLException;
import java.util.Random;

import db.IQuestionsDb;


/**
 * @author Boaz
 *	To be extended by a different classes that generate different type of question patterns 
 */
public abstract class QuestionPattern {

	protected final int _patternId;			//	unique pattern ID correlated with the DB
	protected String _pattern;		//	i.e. "the inflation in <Country_name> is ?"
	protected Level _patternLevel;	//	is not changed during the game
	protected int _numberOfTimesUsed;		//	To be updated during the game, 
	protected int _numberOfSuccesses;		//	and to be saved in the DB, 
	protected double _patternStatistics;	//	at the end of the game


	//	CTOR
	protected QuestionPattern(int patternId) {
		_patternId = patternId;
	}


	//	The main method of this class	-	To be implemented by extending class 
	public abstract Question generateNewQuestion(IQuestionsDb db, Random random) throws SQLException;

	//	To be called by a Question instance that has been answered by user	-	for statistics purposes
	public void aQuestionHasBeenAnswered(boolean successfully){
		_numberOfTimesUsed++;
		_numberOfSuccesses += (successfully ? 1 : 0);
		_patternStatistics = _numberOfSuccesses / _numberOfTimesUsed;
	}

	public int getPatternId() {
		return _patternId;
	}

	public String getPattern() {
		return _pattern;
	}


	public void setPattern(String _pattern) {
		this._pattern = _pattern;
	}

	public Level getPatternLevel() {
		return _patternLevel;
	}

	public double getPatternStatistics() {
		return _patternStatistics;
	}

	

	public static enum Level {
		ONE(1.0, 0.9),
		TWO(0.9, 0.8),
		THREE(0.8, 0.7),
		FOUR(0.7, 0.6),
		FIVE(0.6, 0.5),
		SIX(0.5, 0.4),
		SEVEN(0.4, 0.3),
		EIGHT(0.3, 0.2),
		NINE(0.2, 0.1),
		TEN(0.1, 0.05),
		TOP(0.1, 0.0);

		public final double ceilValue;
		public final double floorValue;
		
		private Level(double cValue, double fValue){
			ceilValue = cValue;
			floorValue = fValue;
		}
		
		//		This method is called to evaluate the right level of a question pattern based on it's success statistics
		public static Level getLevelOf(double patternStatistics) {
			int successOutOf10 = (int) ((patternStatistics * 100) % 10 );

			switch(successOutOf10){
			case 10:	return Level.ONE;
			case 9:		return Level.TWO;
			case 8:		return Level.THREE;
			case 7:		return Level.FOUR;
			case 6:		return Level.FIVE;
			case 5: 	return Level.SIX;
			case 4:		return Level.SEVEN;
			case 3:		return Level.EIGHT;
			case 2:		return Level.NINE;
			case 1:		return Level.TEN;
			case 0:		return Level.TOP;
			default:	return Level.ONE;
			}
		}
	}

}
